package com.hhkj.admin.student.service.biz.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.admin.course.model.dopo.CoursePo;
import com.hhkj.admin.course.service.base.CourseService;
import com.hhkj.admin.course.service.extend.CourseBankTypeExtendService;
import com.hhkj.admin.student.common.cont.StudentConst;
import com.hhkj.admin.student.domain.StudentCourse;
import com.hhkj.admin.student.model.dopo.StudentCourseDo;
import com.hhkj.admin.student.model.dopo.StudentCoursePo;
import com.hhkj.admin.student.model.req.StudentCourseReq;
import com.hhkj.admin.student.model.res.StudentCourseRes;
import com.hhkj.admin.student.service.base.StudentCourseService;
import com.hhkj.admin.student.service.biz.StudentCourseBizService;
import com.hhkj.admin.student.service.extend.StudentCourseExtendService;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学员科目BizService接口
 *
 * @author ruoyi
 * @since 2023-06-08
 */
@Service
public class StudentCourseBizServiceImpl
        extends BaseBizServiceImpl<StudentCourse, StudentCourseReq, StudentCourseRes, StudentCourseDo, StudentCoursePo, StudentCourseService, StudentCourseExtendService>
        implements StudentCourseBizService {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseBankTypeExtendService bankTypeExtendService;

    @Override
    public boolean updateStatus(StudentCourseReq req) {
        if (ObjectUtil.isEmpty(req.getId())
                || StrUtil.isBlank(req.getStatus())) {
            throw new GlobalException("参数错误");
        }
        StudentCourse studentCoursePo = this.getService().getById(req.getId());
        if (ObjectUtil.isEmpty(studentCoursePo)) {
            throw new GlobalException("信息不存在，请刷新页面后重试");
        }
        if (StudentConst.AuditCode.STATUS_NO_CHECK.getCode().equals(studentCoursePo.getStatus())
                || StudentConst.AuditCode.STATUS_OUT_TIME.getCode().equals(studentCoursePo.getStatus())) {
            throw new GlobalException("当前状态无法操作");
        }
        if (StudentConst.AuditCode.STATUS_DISABLED.getCode().equals(req.getStatus())) {
            if (StudentConst.AuditCode.STATUS_DISABLED.getCode().equals(studentCoursePo.getStatus())) {
                throw new GlobalException("已禁用，请勿重复操作");
            }
        } else if (StudentConst.AuditCode.STATUS_PASS.getCode().equals(req.getStatus())) {
            if (StudentConst.AuditCode.STATUS_PASS.getCode().equals(studentCoursePo.getStatus())) {
                throw new GlobalException("已启用，请勿重复操作");
            }
        } else {
            throw new GlobalException("参数错误");
        }
        LambdaUpdateWrapper<StudentCourse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StudentCourse::getId, req.getId())
                .set(StudentCourse::getStatus, req.getStatus());
        return getService().update(updateWrapper);
    }

    @Override
    public boolean update(StudentCourseReq req) {
        if (ObjectUtil.isEmpty(req.getCourseId())
                || ObjectUtil.isEmpty(req.getCourseBankTypeId())) {
            throw new GlobalException("参数错误");
        }
        StudentCourse studentCoursePo = this.getService().getById(req.getId());
        if (ObjectUtil.isEmpty(studentCoursePo)) {
            throw new GlobalException("信息不存在，请刷新页面后重试");
        }
//        if (StudentConst.AuditCode.STATUS_PASS.getCode().equals(studentCoursePo.getStatus())) {
//            throw new GlobalException("当前状态不支持修改信息");
//        }
        // 判断修改时间
        if (ObjectUtil.isNotEmpty(req.getLineDate()) && req.getLineDate().before(studentCoursePo.getStartDate())) {
            throw new GlobalException("到期时间不能超过开始时间");
        }
        // 检查科目信息
        CoursePo coursePo = courseService.getLevelNumThirdPoById(req.getCourseId());
        if (ObjectUtil.isEmpty(coursePo)) {
            throw new GlobalException("科目信息不存在");
        }
        // 检查题库类型信息
        bankTypeExtendService.getByCourseIdAndIdNotDisable(coursePo.getParentId(), req.getCourseBankTypeId());

        LambdaUpdateWrapper<StudentCourse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StudentCourse::getId, req.getId())
                .set(StudentCourse::getCourseId, req.getCourseId())
                .set(StudentCourse::getCourseBankTypeId, req.getCourseBankTypeId())
                .set(ObjectUtil.isNotEmpty(req.getLineDate()), StudentCourse::getLineDate, req.getLineDate());
        return getService().update(updateWrapper);
    }
}
